package rnClasses;

public class Calculator {

	private String textoTela;

	private Command opBuffer;

	private StringBuilder textoTelaBuilder;

	private int valBuffer;

	public final String getTextoTela() {
		return textoTela;
	}

	public final void setTextoTela(String textoTela) {
		this.textoTela = textoTela;
	}

	public final int getValBuffer() {
		return valBuffer;
	}

	public final void setValBuffer(int valBuffer) {
		this.valBuffer = valBuffer;
	}

	public Calculator() {
		textoTela = "0,";
	}

	public String equalsClick() {
		String returnString = opBuffer.Execute(valBuffer, Integer
				.valueOf(textoTela));
		opBuffer = null;
		valBuffer = 0;
		return returnString;
	}

	public void somaClick() {
		if (opBuffer == null) {
			opBuffer = new SomaCommnad(valBuffer);
			textoTelaBuilder = null;
		}
	}

	public void divClick() {
		if (opBuffer == null) {
			opBuffer = new DivCommand(valBuffer);
			textoTelaBuilder = null;
		}
	}

	public void subClick() {
		if (opBuffer == null){
		opBuffer = new SubtraiCommand(valBuffer);
		textoTelaBuilder = null;
		}
	}

	public void multClick() {
		if (opBuffer == null){
		opBuffer = new MultCommand(valBuffer);
		textoTelaBuilder = null;
		}
	}

	public void numberClick(String buttonText) {
		if (textoTelaBuilder == null)
			textoTelaBuilder = new StringBuilder();
		textoTelaBuilder.append(buttonText);
		valBuffer = Integer.valueOf(textoTelaBuilder.toString());
		textoTela = textoTelaBuilder.toString();
	}

	private interface Command {
		public String Execute(int valor1, int valor2);
	}

	private abstract class AbstractCommand implements Command {
		private final int valBuffer;

		public AbstractCommand(int valBuffer) {
			super();
			this.valBuffer = valBuffer;
		}

		protected final int getValBuffer() {
			return valBuffer;
		}

	}

	private class SomaCommnad extends AbstractCommand implements Command {

		public SomaCommnad(int valBuffer) {
			super(valBuffer);
			// TODO Auto-generated constructor stub
		}

		public String Execute(int valor1, int valor2) {
			return String.valueOf(valor1 + valor2);
		}
	}

	private class SubtraiCommand extends AbstractCommand implements Command {

		public SubtraiCommand(int valBuffer) {
			super(valBuffer);
			// TODO Auto-generated constructor stub
		}

		public String Execute(int valor1, int valor2) {
			return String.valueOf(valor1 - valor2);
		}

	}

	private class MultCommand extends AbstractCommand implements Command {

		public MultCommand(int valBuffer) {
			super(valBuffer);
			// TODO Auto-generated constructor stub
		}

		public String Execute(int valor1, int valor2) {
			return String.valueOf(valor1 * valor2);
		}
	}

	private class DivCommand extends AbstractCommand implements Command {

		public DivCommand(int valBuffer) {
			super(valBuffer);
			// TODO Auto-generated constructor stub
		}

		public String Execute(int valor1, int valor2) {
			return valor2 != 0 ? String.valueOf(valor1 / valor2)
					: "Indeterminado";
		}

	}

}
